package com.cn.goodsadmin.dao.impl;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.BaseDao;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.dao.IWordsDao;
import com.cn.goodsadmin.po.Words;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 留言的具体实现
 *
 * @author fly
 * @create 2017-11-28 17:17
 **/

@Repository("WordsDaoImpl")
public class WordsDaoImpl extends BaseDao<Words> implements IWordsDao {
    @Override
    public Words findWordsByMsgId(String msgId) throws Exception {
        StringBuilder builder=new StringBuilder();
        builder.append("from Words w where w.cMsgId='"+msgId+"'");
        List<Words> words=find(builder.toString());
        if(words!=null&&words.size()>0){
            return words.get(0);
        }
        return null;
    }

    @Override
    public List<Words> findCallBackWords(Words words) throws Exception {
        String mid=words.getcMsgId();
        String touser=words.getcTousername();
        StringBuilder builder=new StringBuilder();
        builder.append("from Words w where w.cMsgId='"+mid+"'");
        builder.append(" and w.cFromusername='"+touser+"'");
        builder.append(" order by w.cGctime desc");
        return find(builder.toString());
    }

    @Override
    public PageQueryResult<DestDTO> findAllWordsbyAppidAndOpenId(int pageindex, int pagesize, String wxnumber, String openid) throws Exception {
        List<Object> paraObject=new ArrayList<Object>();
        StringBuilder builder=new StringBuilder();
        builder.append("from Words w where w.cTousername=?0");
        paraObject.add(wxnumber);
        builder.append(" and w.cFromusername=?1");
        paraObject.add(openid);
        builder.append("order by w.cGctime desc");
        return commonQuery(builder.toString(),paraObject.toArray(),pageindex,pagesize,DestDTO.class);
    }

    @Override
    public Words findLastUserCallBack(String openid, String  wxnumber) throws Exception {
        Criteria cri = getSessionFactory().getCurrentSession().createCriteria(Words.class);
        cri.add(Restrictions.eq("cFromusername", openid));
        cri.add(Restrictions.eq("cTousername", wxnumber));
        cri.addOrder(Order.desc("cGctime"));
        cri.setMaxResults(1);
        List<Words> list=cri.list();
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public PageQueryResult<DestDTO> findAllWordsbyAppid(int pageindex, int pagesize, String  wxnumber) throws Exception {
        List<Object> paraObject=new ArrayList<Object>();
        StringBuilder builder=new StringBuilder();
        builder.append("from Words w where w.cTousername=?0");
        builder.append("order by w.cGctime desc");
        paraObject.add(wxnumber);
        return commonQuery(builder.toString(),paraObject.toArray(),pageindex,pagesize,DestDTO.class);
    }
}
